package cn.itsource.xhy.order.service.impl;

import cn.itsource.xhy.basic.domain.Point;
import cn.itsource.xhy.basic.service.impl.BaseServiceImpl;
import cn.itsource.xhy.order.domain.OrderAddress;
import cn.itsource.xhy.order.domain.OrderProduct;
import cn.itsource.xhy.order.mapper.OrderAddressMapper;
import cn.itsource.xhy.order.mapper.OrderProductMapper;
import cn.itsource.xhy.order.service.IOrderProductService;
import cn.itsource.xhy.org.domain.Shop;
import cn.itsource.xhy.org.mapper.ShopMapper;
import cn.itsource.xhy.pay.contants.BusinessContants;
import cn.itsource.xhy.pay.domain.PayBill;
import cn.itsource.xhy.pay.service.IPayBillService;
import cn.itsource.xhy.pro.domain.Product;
import cn.itsource.xhy.pro.mapper.ProductMapper;
import cn.itsource.xhy.quartz.domain.QuartzJobInfo;
import cn.itsource.xhy.quartz.service.IQuartzService;
import cn.itsource.xhy.user.domain.User;
import cn.itsource.xhy.user.domain.UserAddress;
import cn.itsource.xhy.user.mapper.UserAddressMapper;
import cn.itsource.xhy.utils.CodeGenerateUtils;
import cn.itsource.xhy.utils.DistanceUtil;
import cn.itsource.xhy.utils.LoginContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IQuartzService quartzService;


    @Override
    @Transactional
    public String createOrder(Map<String, String> map, HttpServletRequest request) {
        Long productId = Long.valueOf(map.get("product_id"));
        Long addressId = Long.valueOf(map.get("address_id"));
        String paymentWay = map.get("payment_way");
        User user = (User) LoginContext.getObject(request);
        //拿到商品对象
        Product product = productMapper.findByProId(productId);
        //给销量加一
        product.setSalecount(product.getSalecount()+1);
        // 修改
        productMapper.update(product);

        //创建订单
        OrderProduct orderProduct = createOrderProduct(product,user);
        //查到用户填写的地址
        UserAddress userAddress = userAddressMapper.findById(addressId);
        //使用工具类将地址转换为经纬度
        Point point = DistanceUtil.getPoint(userAddress.getFullAddress());
        //获取门店信息
        List<Shop> shops = shopMapper.findAll();
        //使用工具类获取直线距离最近的门店
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        //拿到店铺的id
        orderProduct.setShop_id(shop.getId());
        //保存信息
        orderProductMapper.save(orderProduct);

        //保存订单地址
        OrderAddress orderAddress = userAddressToOrderAddress(userAddress);
        // 补全信息
        orderAddress.setOrder_id(orderProduct.getId());
        orderAddress.setOrderSn(orderProduct.getOrderSn());
        //保存信息
        orderAddressMapper.save(orderAddress);

        //支付订单
        PayBill payBill = createPayBil(orderProduct);
        // 补全信息
        payBill.setPayChannel(paymentWay);
        payBill.setBusinessType(BusinessContants.ORDER_PRODUCT);
        payBill.setBusinessKey(orderProduct.getId());
        // 生成支付单子
        payBillService.save(payBill);
        // 支付单
        String result = payBillService.pay(payBill);

        //创建定时任务
        QuartzJobInfo jobInfo = new QuartzJobInfo();
        //设置任务名
        jobInfo.setJobName(payBill.getOrderSn());
        Map<String, Object> param = new HashMap<>();
        param.put("orderSn",payBill.getOrderSn());
        //携带参数
        jobInfo.setParams(param);
        //设置触发时间
        jobInfo.setFireDate(payBill.getLastPayTime());
        //调用方法将参数传过去
        quartzService.addJob(jobInfo);

        return result;

    }

    @Override
    public OrderProduct findByOrderSn(String orderSn) {
        return orderProductMapper.findByOrderSn(orderSn);
    }

    private PayBill createPayBil(OrderProduct orderProduct) {
        PayBill payBill = new PayBill();
        payBill.setDigest(orderProduct.getDigest());
        payBill.setMoney(orderProduct.getPrice());
        /*默认0:待支付  1支付成功  -1:取消订单 */
        payBill.setState(0);
        payBill.setLastPayTime(orderProduct.getLastPayTime());
        payBill.setCreateTime(new Date());
        payBill.setUser_id(orderProduct.getUser_id());
        payBill.setShop_id(orderProduct.getShop_id());
        payBill.setOrderSn(orderProduct.getOrderSn());
        return payBill;
    }

    // 组装订单地址
    private OrderAddress userAddressToOrderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setCreateTime(new Date());
        return orderAddress;
    }

    // 组装订单
    private OrderProduct createOrderProduct(Product product, User user) {
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setDigest("[服务订单]购买"+product.getName()+"服务！");
        orderProduct.setState(0);
        orderProduct.setPrice(product.getSaleprice());
        orderProduct.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        orderProduct.setLastPayTime(new Date(System.currentTimeMillis()+60*1000*2));
        orderProduct.setProduct_id(product.getId());
        orderProduct.setUser_id(user.getId());
        return orderProduct;
    }



}
